package com.oncecloud.devops.service.sample.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.oncecloud.devops.service.sample.bean.Result;
import com.oncecloud.devops.service.sample.common.Common;
import com.oncecloud.devops.service.sample.common.GitLab;
import com.oncecloud.devops.service.sample.core.CiCore;
import com.oncecloud.devops.service.sample.service.UserService;

import net.sf.json.util.JSONUtils;

@RestController
@RequestMapping("/ci")
public class CiController {
	@Autowired
	private CiCore ciCore;
	@RequestMapping(value = "/projects", method = { RequestMethod.GET })
	@ResponseBody
	public String getGitProjects(@RequestParam(value = "protocol", required = true) String http_protocol,
			@RequestParam(value = "hostname", required = true) String gitLab_url,
			@RequestParam(value = "port", required = true) String gitLab_port,
			@RequestParam(value = "token", required = true) String private_token){
		GitLab gitLab = new GitLab();
		gitLab.setGitLab_url(gitLab_url);
		gitLab.setGitLab_port(gitLab_port);
		gitLab.setHttp_protocol(http_protocol);
		Result result = ciCore.getGitProjects(gitLab, private_token);
		if(result.isSuccess()){
			return result.getData().toString();
		}else{
			JSONObject object = new JSONObject();
			object.put("error", result.getMessage());
			return object.toJSONString();
		}
		
	}
}
